package com.ogawa.a7517.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.reslib.Constants;
import com.example.reslib.utils.PreferenceWrapper;
import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;

public class LanguageFragment extends BaseFragment implements View.OnClickListener {
    private String language;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_language;
    }

    @Override
    public void initView(View view) {
        TextView tvChinese = view.findViewById(R.id.tv_simple_chinese);
        tvChinese.setOnClickListener(this);
        TextView tvEnglish = view.findViewById(R.id.tv_english);
        tvEnglish.setOnClickListener(this);
        TextView tvCantonese = view.findViewById(R.id.tv_cantonese);
        tvCantonese.setOnClickListener(this);
        view.findViewById(R.id.iv_confirm).setOnClickListener(this);
        language = new PreferenceWrapper().getStringValue(Constant.LANGUAGE, Constants.ZH);
        tvChinese.setSelected(Constants.ZH.equals(language));
        tvEnglish.setSelected(Constants.EN.equals(language));
        tvCantonese.setSelected(Constants.HK.equals(language));
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getString(R.string.language));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_simple_chinese) {
            setLanguage(Constants.ZH);
        } else if (id == R.id.tv_english) {
            setLanguage(Constants.EN);
        } else if (id == R.id.tv_cantonese) {
            setLanguage(Constants.HK);
        } else if (id == R.id.iv_confirm) {
            activity7517.popBackStack();
        }
    }

    private void setLanguage(String lan) {
        if (language.equals(lan)) {
            activity7517.popBackStack(MainFragment.class.getName(), 0);
        } else {
            activity7517.switchLanguage(lan);
        }
    }
}
